//
//  MTUI.m
//  FaceLink
//
//  Created by Apple on 2023/7/18.
//

#import "MTUI.h"

@implementation MTUI

+ (UIView *)viewWithFrame:(CGRect)frame
          backgroundColor:(UIColor *)backgroundColor{
    
    UIView *view = [[UIView alloc] init];
    view.frame = frame;
    view.backgroundColor = backgroundColor;
    return view;
}

+ (UILabel *)labelWithFont:(nullable UIFont *)font
             textAlignment:(NSTextAlignment)alignment
                 textColor:(nullable UIColor *)textColor
             numberOflines:(NSInteger)numberOflines
                      text:(NSString *)text
                    enable:(BOOL)enable{
    
    UILabel *lab = [[UILabel alloc] init];
    lab.font = font;
    lab.textAlignment = alignment;
    lab.textColor = textColor;
    lab.userInteractionEnabled = enable;
    lab.text = text;
    lab.numberOfLines = numberOflines;
    return lab;
}

+ (UIImageView *)imageViewWithImage:(nullable UIImage *)image
                        contentMode:(UIViewContentMode)mode{
    
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.image = image;
    imgView.contentMode = mode;
    return imgView;
}

+ (UIButton *)btnWithTitle:(nullable NSString *)title
                 titleFont:(nullable UIFont *)titleFont
                titleColor:(nullable UIColor *)titleColor
           backgroundColor:(nullable UIColor *)backGroundColor
                    target:(nullable id)target
                    action:(nullable SEL)action{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = titleFont;
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.backgroundColor = backGroundColor;
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
    
}

+ (UIButton *)btnWithImage:(nullable UIImage *)image
                    target:(nullable id)target
                    action:(nullable SEL)action{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:image forState:UIControlStateNormal];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
    
}

+ (UIButton *)btnWithNormalImage:(nullable UIImage*)normalImg
                     selectedImg:(nullable UIImage *)selectImg
                          target:(nullable id)target
                          action:(nullable SEL)action{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:normalImg forState:UIControlStateNormal];
    [btn setImage:selectImg forState:UIControlStateSelected];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
    
}

+ (UIButton *)btnWithType:(UIButtonType)btnType
                    title:(nullable NSString *)title
                titleFont:(nullable UIFont *)titleFont
               titleColor:(nullable UIColor *)titleColor
          backgroundColor:(nullable UIColor *)backGroundColor
                   target:(nullable id)target
                   action:(nullable SEL)action{
    
    UIButton *btn = [UIButton buttonWithType:btnType];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = titleFont;
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.backgroundColor = backGroundColor;
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
     
    return btn;
}

+(UITableView *)tableViewWithFrame:(CGRect)frame
                             style:(UITableViewStyle)style
                          delegate:(nullable id)delegate
                        dataSource:(nullable id)dataSource
                         rowHeight:(CGFloat)rowHeight
                estimatedRowHeight:(CGFloat)estimatedRowHeight
               sectionHeaderHeight:(CGFloat)sectionHeaderHeight
               sectionFooterHeight:(CGFloat)sectionFooterHeight
      estimatedSectionHeaderHeight:(CGFloat)estimatedSectionHeaderHeight
      estimatedSectionFooterHeight:(CGFloat)estimatedSectionFooterHeight
    showsHorizontalScrollIndicator:(BOOL)showsHorizontalScrollIndicator
      showsVerticalScrollIndicator:(BOOL)showsVerticalScrollIndicator
                    separatorStyle:(UITableViewCellSeparatorStyle)separatorStyle
                   backgroundColor:(UIColor *)backgroundColor{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:style];
    tableView.delegate = delegate;
    tableView.dataSource = dataSource;
    tableView.rowHeight = rowHeight;
    tableView.estimatedRowHeight = estimatedRowHeight;
    tableView.sectionHeaderHeight = sectionHeaderHeight;
    tableView.sectionFooterHeight = sectionFooterHeight;
    tableView.estimatedSectionHeaderHeight = estimatedSectionHeaderHeight;
    tableView.estimatedSectionFooterHeight = estimatedSectionFooterHeight;
    tableView.showsHorizontalScrollIndicator = showsHorizontalScrollIndicator;
    tableView.showsVerticalScrollIndicator = showsVerticalScrollIndicator;
    tableView.separatorStyle = separatorStyle;
    tableView.backgroundColor = backgroundColor;
    
    if (@available(iOS 11.0, *)) {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    if (@available(iOS 13.0, *)) {
        tableView.automaticallyAdjustsScrollIndicatorInsets = false;
    } else {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    if (@available(iOS 15.0, *)) {
        tableView.sectionHeaderTopPadding = 0.01;
    }
    
    return tableView;
    
}

+ (UITextField *)textFieldWithFrame:(CGRect)frame
                               font:(UIFont *)font
                      textAlignment:(NSTextAlignment)textAlignment
                          textColor:(UIColor *)textColor
                        borderStyle:(UITextBorderStyle)borderStyle
                        placeholder:(NSString *)placeholder
                           delegate:(nullable id)delegate{
    
    UITextField *textField = [[UITextField alloc] init];
    textField.frame = frame;
    textField.font = font;
    textField.textAlignment = textAlignment;
    textField.textColor = textColor;
    textField.borderStyle = borderStyle;
    textField.placeholder = placeholder;
    textField.delegate = delegate;
    
    return textField;
}

+ (UITextView *)textViewWithFrame:(CGRect)frame
                             font:(UIFont *)font
                    textAlignment:(NSTextAlignment)textAlignment
                        textColor:(UIColor *)textColor
                         delegate:(nullable id)delegate
                         editable:(BOOL)editable{
    
    UITextView *textView = [[UITextView alloc] init];
    textView.font = font;
    textView.textAlignment = textAlignment;
    textView.textColor = textColor;
    textView.editable = editable;
    textView.delegate = delegate;
    return textView;
}

+ (UIScrollView *)scrollViewWithFrame:(CGRect)frame
       showsHorizontalScrollIndicator:(BOOL)showsHorizontalScrollIndicator
         showsVerticalScrollIndicator:(BOOL)showsVerticalScrollIndicator
                        pagingEnabled:(BOOL)pagingEnabled
                        scrollEnabled:(BOOL)scrollEnabled
                              bounces:(BOOL)bounces
                             delegate:(nullable id)delegate{
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
    scrollView.showsHorizontalScrollIndicator = showsHorizontalScrollIndicator;
    scrollView.showsVerticalScrollIndicator = showsVerticalScrollIndicator;
    scrollView.pagingEnabled = pagingEnabled;
    scrollView.scrollEnabled = scrollEnabled;
    scrollView.bounces = bounces;
    scrollView.delegate = delegate;
    
    return scrollView;
}

+ (UICollectionView *)collectionViewWithFrame:(CGRect)frame
                                   flowlayout:(UICollectionViewFlowLayout *)flowlayout
                                     delegate:(nullable id)delegate
                                   dataSource:(nullable id)dataSource{
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowlayout];
    collectionView.delegate = delegate;
    collectionView.dataSource = dataSource;
    
    if (@available(iOS 11.0, *)) {
        collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    if(@available(iOS 13.0, *)){
        collectionView.automaticallyAdjustsScrollIndicatorInsets = false;
    }
    
    return collectionView;
}

+ (UICollectionViewFlowLayout *)flowLayoutWithLineSpace:(CGFloat)lineSpace
                                             interSpace:(CGFloat)interSpace
                                               itemSize:(CGSize)itemSize
                                      estimatedItemSize:(CGSize)estimatedItemSize
                                        scrollDirection:(UICollectionViewScrollDirection)scrollDirection
                                    headerReferenceSize:(CGSize)headerReferenceSize
                                    footerReferenceSize:(CGSize)footerReferenceSize
                                           sectionInset:(UIEdgeInsets)sectionInset{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = lineSpace;
    flowLayout.minimumInteritemSpacing = interSpace;
    flowLayout.itemSize = itemSize;
    flowLayout.estimatedItemSize = estimatedItemSize;
    flowLayout.scrollDirection = scrollDirection;
    flowLayout.headerReferenceSize = headerReferenceSize;
    flowLayout.footerReferenceSize = footerReferenceSize;
    
    return flowLayout;
}

@end
